package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"novel-mgr/cache"
	"novel-mgr/models"
	"path/filepath"
	"strings"
	"time"
)

type MgrController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MgrController) Add() {
	id, _ := c.GetInt("id")
	name := c.GetString("name")
	price, _ := c.GetFloat("price")
	cut := c.GetString("cut")
	img := c.GetString("img")
	mgr := models.Mgr{
		Id:    id,
		Name:  name,
		Price: price,
		Cut:   cut,
		Img:   img,
	}
	if name == "" || cut == "" || img == "" && id == 0 || price == 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	err := models.DB.Create(&mgr).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *MgrController) Upload() {
	file, header, err := c.GetFile("img")
	if header.Size > 5*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "图片大小不能超过5m",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	liu := map[string]bool{
		".PDF": true,
		".PNG": true,
		".JPG": true,
	}
	yu := strings.ToTitle(filepath.Ext(header.Filename))
	if liu[yu] == false {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "格式不一致",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: "http://127.0.0.1:8080/" + path,
		}
		c.ServeJSON()
		return
	}
}
func (c *MgrController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var mm []models.Mgr
	err := models.DB.Find(&mm).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		key := fmt.Sprintf("mgr_list_%d", models.Mgr{}.Id)
		res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
		if res != "" {
			json.Unmarshal([]byte(res), mm)
		} else {
			rest, _ := json.Marshal(mm)
			cache.Rdb.Set(cache.Ctx, key, rest, time.Hour*2).Result()
		}
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: mm,
		}
		c.ServeJSON()
		return
	}
}
func (c *MgrController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Mgr{}, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
func (c *MgrController) Emil() {
	var mm []models.Mgr
	id, _ := c.GetInt("id")
	err := models.DB.Where("id=?", id).First(&mm).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "详情查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		key := fmt.Sprintf("mgr_emil_%d", models.Mgr{}.Id)
		res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
		if res != "" {
			json.Unmarshal([]byte(res), mm)
		} else {
			rest, _ := json.Marshal(mm)
			cache.Rdb.Set(cache.Ctx, key, rest, time.Hour*2).Result()
		}
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "详情查询成功",
			Data: mm,
		}
		c.ServeJSON()
		return
	}
}
